﻿using GlobalMouseHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int MouseEvent_Absolute = 0x8000;
        const int MouserEvent_Hwheel = 0x01000;
        const int MouseEvent_Move = 0x0001;
        const int MouseEvent_Move_noCoalesce = 0x2000;
        const int MouseEvent_LeftDown = 0x0002;
        const int MouseEvent_LeftUp = 0x0004;
        const int MouseEvent_MiddleDown = 0x0020;
        const int MouseEvent_MiddleUp = 0x0040;
        const int MouseEvent_RightDown = 0x0008;
        const int MouseEvent_RightUp = 0x0010;
        const int MouseEvent_Wheel = 0x0800;
        const int MousseEvent_XUp = 0x0100;
        const int MousseEvent_XDown = 0x0080;

        const int INPUT_MOUSE = 0;
        const int INPUT_KEYBOARD = 1;
        const int INPUT_HARDWARE = 2;
        const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
        const uint KEYEVENTF_KEYUP = 0x0002;
        const uint KEYEVENTF_UNICODE = 0x0004;
        const uint KEYEVENTF_SCANCODE = 0x0008;

        struct INPUT
        {
            public int type;
            public InputUnion u;
        }

        [StructLayout(LayoutKind.Explicit)]
        struct InputUnion
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public KEYBDINPUT ki;
            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            /*Virtual Key code.  Must be from 1-254.  If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
            public ushort wVk;
            /*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
            public ushort wScan;
            /*Specifies various aspects of a keystroke.  See the KEYEVENTF_ constants for more information.*/
            public uint dwFlags;
            /*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
            public uint time;
            /*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            public uint uMsg;
            public ushort wParamL;
            public ushort wParamH;
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetMessageExtraInfo();

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);




        MouseHook mouse = new MouseHook();
        public Form1()
        {
            //InitializeComponent();
            mouse.OnMouseActivity += new MouseEventHandler(mouse_OnMouseActivity);
            mouse.Start();
        }

        void mouse_OnMouseActivity(object sender, MouseEventArgs e)
        {

            string str = "X:" + e.X + "  Y:" + e.Y;
            this.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            /*
            dx = x * (65335/ScreenWidth) //x,y为像素坐标。
dy = y * (65335/ScreenHeight)//ScreenWidth和ScreenHeight，其实是当前显示器的分辨率，获得方法是ScreenWidth=Screen.PrimaryScreen.WorkingArea.Width；

ScreenHeight=Screen.PrimaryScreen.WorkingArea.Height；

    */
            //mouse.Stop();

            Point p = Point.Empty;// this.Convert(new Point() { X = int.Parse(textBox1.Text), Y = int.Parse(textBox2.Text) });

            MOUSEINPUT myMinput = new MOUSEINPUT();

            myMinput.dx = p.X;
            myMinput.dy = p.Y;
            myMinput.mouseData = 0;
            myMinput.dwFlags = MouseEvent_Absolute | MouseEvent_Move | MouseEvent_LeftDown | MouseEvent_LeftUp;

            myMinput.time = 0;



            INPUT[] inputs = new INPUT[]
            {
                 new INPUT{ type = INPUT_MOUSE,
                     u = new InputUnion
                     { 
                         mi = myMinput
                     }
                 }
            };


            UInt32 result = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0].GetType()));
            if (result == 0)
            {
                MessageBox.Show("fail");
            }

            Thread.Sleep(2000);

            //输入dud2850


            inputs = new INPUT[]
            {
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.U,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },                 
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D2,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D8,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D5,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 },
                 new INPUT{ type = INPUT_KEYBOARD,
                     u = new InputUnion
                     { 
                         ki = new KEYBDINPUT(){
                            wVk = (ushort)Keys.D0,
                            wScan = 0,
                            dwFlags = 0,
                            dwExtraInfo = GetMessageExtraInfo(),
                         }
                     }
                 }
            };


            result = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0].GetType()));
            if (result == 0)
            {
                MessageBox.Show("fail");
            }

        }


        private Point Convert(Point point)
        {
            Point returnValue = new Point();
            returnValue.X = point.X * (65335 / Screen.PrimaryScreen.WorkingArea.Width);
            returnValue.Y = point.Y * (65335 / Screen.PrimaryScreen.WorkingArea.Height);

            return returnValue;
        }

    }
}
